package com.gxa.controller;

import com.gxa.pojo.Permission;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.LogService;
import com.gxa.service.PermissionService;
import com.gxa.service.RoleService;
import com.gxa.utils.JsonResult;
import com.gxa.utils.PowerTreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;

    @Autowired
    LogService logService;
    JsonResult jsonResult;

    @ResponseBody
    @RequestMapping("/rolePage")
    public Map findUserByPage(@RequestBody Map map){
        jsonResult=new JsonResult();
        String number=map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        List<Role> roles = roleService.selectRoleByPage(offset, limit);
        Integer count = roleService.selectAllRole();
        jsonResult.setData(roles);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/selectRoleAll")
    public List<Role> selectRoleAll(){
        jsonResult=new JsonResult();
        List<Role> roles = roleService.selectRoleAll();
        return roles;
    }

    @ResponseBody
    @RequestMapping("/selectRollByUserId")
    public Map selectRollByUserId(@RequestBody Map map){
        jsonResult=new JsonResult();
        List<Role> roles = roleService.selectRoleAll();
        List<Role> roleList = roleService.selectRollByUserId(map);
        jsonResult.setData(roleList);
        jsonResult.put("roles",roles);
        jsonResult.setCode("200");
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/insertRole")
    public Map insertRole(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = roleService.insertRole(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/deleteRole")
    public Map deleteUser(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = roleService.deleteRole(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/roleEdit")
    public Map updateUser(@RequestBody Map map){
        jsonResult =new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = roleService.updateRole(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/insertUserRole")
    public Map insertRolePermission(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        if (map.get("userID").toString()!=null){
            String userID = map.get("userID").toString();
            Integer integer1 = roleService.deleteRoleUser(Integer.valueOf(userID));
        }
        Integer integer = roleService.insertUserRole(map);
        if (integer>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("失败");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/findPermissionByRoleId")
    public Map findPermissionByRoleId(@RequestBody Map map){
        jsonResult=new JsonResult();
        String roleID = map.get("roleID").toString();
        List<Permission> permissionByRoleId = roleService.findPermissionByRoleId(Integer.valueOf(roleID));
        List<Permission> treeList = PowerTreeUtil.getTreeList(permissionByRoleId);
        List<Permission> allPermission = permissionService.findAllPermission();
        List<Permission> treeList1 = PowerTreeUtil.getTreeList(allPermission);
        System.out.println(treeList1);
        jsonResult.put("treeList1",treeList1);
        jsonResult.setData(treeList);
        jsonResult.setCode("200");
        jsonResult.setMsg("检索成功");
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/roleState")
    public Map updateUserState(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = roleService.updateRoleState(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("状态修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }
}